<?
class bjMenuManager{
	const MENU_INSTALL_SUCCESS		= 1;
	const MENU_FATAL_ERROR			= 0;
	const MENU_MISSING_DESCRIPTOR	= -1;
	const MENU_ALREADY_INSTALLED  	= -2;	
	const MENU_CAN_UPGRADE			= -3;
	
	private static $instance;
	
	private function __construct() {
	}
	
	public static function getInstance(){
		if(empty(self::$instance)){
			self::$instance = new bjMenuManager();	
		}
		
		return self::$instance;
	}
	
	/**
	 * Updates a menu item. 
	 * @param array $params
	 * @subparam String $parmas->category Category to put menu item under
	 * @subparam String $params-item_name Menu item to put under category. It is what the user would click on in the menu.
	 * @subparam String $params->lang Two letter ISO language code. Eg. 'en'
	 * @subparam String $params->url URL to go to when clicked.	 
	 * @subparam String [$params:keywords[i]] Keywords to search for. Must be alpha characters only, no space
	 * @return array $return
	 * @subreturn String $return->errorCode
	 * @subreturn String $return->msg
	 */	
	public function update($params){
			
	}
		
	/**
	 * Registers a menu item. 
	 * @param array $params
	 * @subparam String $parmas->category Category to put menu item under
	 * @subparam String $params-item_name Menu item to put under category. It is what the user would click on in the menu.
	 * @subparam String $params->lang Two letter ISO language code. Eg. 'en'
	 * @subparam String $params->url URL to go to when clicked.	 
	 * @subparam String [$params:keywords[i]] Keywords to search for. Must be alpha characters only, no space
	 * @return array $return
	 * @subreturn String $return->errorCode
	 * @subreturn String $return->msg
	 */
	public function register($params){
		
		if(empty($params["category"])){
			return array("errorCode" => self::MENU_MISSING_DESCRIPTOR, "msg" => "Missing cateogry.");	
		}

		if(empty($params["item_name"])){
			return array("errorCode" => self::MENU_MISSING_DESCRIPTOR, "msg" => "Missing menu item name.");	
		}

		if(empty($params["lang"])){
			return array("errorCode" => self::MENU_MISSING_DESCRIPTOR, "msg" => "Missing language code.");	
		}

		if(empty($params["url"])){
			return array("errorCode" => self::MENU_MISSING_DESCRIPTOR, "msg" => "Missing menu url.");	
		}
		
		//Check that the menu item / category does not already exist
		$q = Doctrine_Query::create()
			->select("m.menu_id")
    		->from('bjMenuItem m')
    		->leftJoin('m.Translation t')
			->where("t.menu_category = ? AND t.menu_item_name = ? AND t.lang = ?", array($params["category"], $params["item_name"], $params["lang"]))
			->setHydrationMode(Doctrine::HYDRATE_ARRAY)
			->fetchOne();
		
		if(!empty($q)){ //category/item exists
				return array("errorCode" => self::MENU_ALREADY_INSTALLED, "msg" => "Menu item and category already exists for the specified language. Use the update method to update items.");	
		}
				
		//add the category/menu item to the database
		try{
			//Build keyword listing
			$keywords = "";
			foreach($params["keywords"] as $key => $value){
				if(ctype_alnum($value)){
					$keywords = $keywords." ".$value;
				} else {
					return array("errorCode" => self::MENU_FATAL_ERROR, "msg" => "Keyword ".$value." need to only be alpha characters only.");	
				}					
			}
			
			$menuItem = new bjMenuItem();
			$menuItem->Translation[strtolower($params["lang"])]->menu_category = $params["category"];
			$menuItem->Translation[strtolower($params["lang"])]->menu_item_name = $params["item_name"];
			$menuItem->Translation[strtolower($params["lang"])]->menu_keywords = $keywords;
			$menuItem->menu_url = $params["url"];
			$menuItem->save();

			if($menuItem->menu_id > 0){
				return array("errorCode" => self::MENU_INSTALL_SUCCESS, "msg" => "Menu item registered.");							
			}

			return array("errorCode" => self::MENU_FATAL_ERROR, "msg" => "Error with menu registration: could not insert into database.");				

		} catch (Exception $e){
			return array("errorCode" => self::MENU_FATAL_ERROR, "msg" => "Error with menu registration: ".$e->getMessage());				
		}
		
	}
}

?>